package com.an.blog.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.an.blog.admin.domain.bo.BlogTagBo;
import com.an.blog.admin.domain.vo.BlogTagVo;
import com.an.blog.service.IBlogTagService;
import com.an.common.annotation.Log;
import com.an.common.annotation.RepeatSubmit;
import com.an.common.core.controller.BaseController;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.domain.R;
import com.an.common.core.page.TableDataInfo;
import com.an.common.core.validate.AddGroup;
import com.an.common.core.validate.EditGroup;
import com.an.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 博客标签
 *
 * @author Ann
 * @date 2023-05-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/blog/tag")
public class BlogTagController extends BaseController {

    private final IBlogTagService blogTagService;

    /**
     * 查询博客标签列表
     *
     * @param bo 业务对象
     * @return {@link List }<{@link BlogTagVo }>
     * @author: Ann
     * @createDate: 2023/05/18 18:54:37
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:tag:list")
    @GetMapping("/list")
    public TableDataInfo<BlogTagVo> list(BlogTagBo bo, PageQuery pageQuery) {
        return blogTagService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询博客标签下拉列表
     *
     * @return {@link R }<{@link List }<{@link BlogTagVo }>>
     * @author: Ann
     * @createDate: 2023/05/09 18:42:15
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:tag:list")
    @GetMapping("/queryTagList")
    public R<List<BlogTagVo>> queryTagList() {
        return R.ok(blogTagService.queryList(new BlogTagBo()));
    }


    /**
     * 获取博客标签详细信息
     *
     * @param tagId 主键
     */
    @SaCheckPermission("blog:tag:query")
    @GetMapping("/{tagId}")
    public R<BlogTagVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long tagId) {
        return R.ok(blogTagService.queryById(tagId));
    }

    /**
     * 新增博客标签
     */
    @SaCheckPermission("blog:tag:add")
    @Log(title = "标签管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogTagBo bo) {
        return toAjax(blogTagService.insertByBo(bo));
    }

    /**
     * 修改博客标签
     */
    @SaCheckPermission("blog:tag:edit")
    @Log(title = "标签管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogTagBo bo) {
        return toAjax(blogTagService.updateByBo(bo));
    }

    /**
     * 状态修改
     *
     * @param bo 业务对象
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/09 18:39:10
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:tag:edit")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody BlogTagBo bo) {
        return toAjax(blogTagService.updateTagStatus(bo));
    }


    /**
     * 删除博客标签
     *
     * @param tagIds 主键串
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/11 12:34:01
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:tag:remove")
    @Log(title = "标签管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tagIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] tagIds) {
        return toAjax(blogTagService.deleteWithValidByIds(Arrays.asList(tagIds), true));
    }
}
